package com.zz.comframe.servlet;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import treemap.zz.json.JSONArray;

import com.zz.comframe.CommonDataProcesser;
import com.zz.comframe.properties.PropertiesPool;

public class getdetail extends HttpServlet { // 提供零散数据访问，json形式返回记录

	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		this.doPost(request, response);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		response.setContentType("text/html; charset=utf-8");
		PrintWriter out = response.getWriter();
		
		String path = request.getContextPath();
		String basePath = request.getScheme() + "://"
				+ request.getServerName() + ":" + request.getServerPort()
				+ path + "/";

		String filepath = getServletContext().getRealPath("/");

		String id = request.getParameter("id");
		String oper = request.getParameter("subgrid");
		//String term = request.getParameter("term");
		// String sql = "";

		try {
			if (id == null || oper == null || "".equals(id) || "".equals(oper) || !PropertiesPool.SQLInjectionValid(id))
				return;

			String where = " where ";
			if (PropertiesPool.getCommonpro("GRID_ID_COLUMN") != null)
				where += PropertiesPool.getCommonpro("GRID_ID_COLUMN") + "='";
			else
				where += "col0='";
			where = PropertiesPool.SQLInjectionValid(id) ? where + id + "' " : "";

			CommonDataProcesser dp = new CommonDataProcesser();
			String sql = PropertiesPool.getCommonpro("SQL_SELECT_PERINFO") + where;
			JSONArray j = dp.getObjectListAll(sql);
			JSONArray cosarr = dp.getObjectListAll(PropertiesPool.getCommonpro("SQL_SELECT_COSTAB"));
			if (j == null || j.length() > 1)
				return;

			String columns = PropertiesPool.getCommonpro("SUBGRID_COLUMN_INDEX");
			JSONArray jcols = columns != null ? new JSONArray(columns) : null;
			out.print("<input id='cos_list' type='hidden' value='" + cosarr.toString() + "' />");
			out.print("<table id='subgrid_table' class='subgrid_table'>");
			out.print("<tr><td style='width:50px'></td><td>");
			out.print("<table id='subgrid_table'>");
			out.print("<div>&nbsp;</div>");
			for (int i = 0; i < jcols.length(); i++) {
				String name = jcols.getJSONObject(i).getString("name");
				String col = jcols.getJSONObject(i).getString("col");

				String value = "";
				if (j.getJSONObject(0).has(col.toLowerCase()))
					value = j.getJSONObject(0).get(col.toLowerCase()).toString();
				// 检查是否是类别字段，是用COSTAB相应名称代替
				if (PropertiesPool.getCommonpro("COS_COLUMN_NAME").equalsIgnoreCase(col))
					for (int k = 0; k < cosarr.length(); k++)
						if (value.equals(cosarr.getJSONObject(k).getString("cosid"))) {
							value = cosarr.getJSONObject(k).getString("cosname");
							break;
						}
				out.print("<tr>");
				out.print("<td><b>" + (name != null ? name : "") + ":</b></td>");
				out.print("<td>" + (value != null ? value : "") + "</td>");
				out.print("</tr>");
			}

			out.print("</table></td>");
			out.print("<td><div class='img-wrapper'><div><div>");
			out.print("<img src='" + PropertiesPool.getCommonpro("PHOTO_URL") + j.getJSONObject(0).get(PropertiesPool.getCommonpro("PHOTO_NAME_COLUMN").toLowerCase()) + ".jpg'/>"); // http://192.167.1.193:8081/photo/20061297.jpg
			out.print("</div></div></div></td><td>");

			List imgfiles = null;
			String step = request.getParameter("step");

			out.print("<a target='_blank' href='/jsp/pbimg.jsp'>添加护照照片</a><br />");
			
			out.print("<div style=\"margin: 20px;border: 1px solid #aaa;\">");
			//<b class="xtop"><b class="xb1"></b><b class="xb2"></b><b
			//	class="xb3"></b><b class="xb4"></b>
			//</b>
			out.print("<div class=\"xboxcontent\">");

			//搜索设置护照现有图片
			String pbid = PropertiesPool.getCommonpro("PBID_COLUMN_NAME")==null ? 
					null : j.getJSONObject(0).get(PropertiesPool.getCommonpro("PBID_COLUMN_NAME")).toString();
			if (pbid == null ){
				response.sendRedirect("pbimglist.jsp");
			}else {
				File dir = new File((new File(filepath)).getParent() + "/pbimg");
				if (dir.isDirectory()) {
					String[] fileList = dir.list();
					Arrays.sort(fileList, String.CASE_INSENSITIVE_ORDER);
					for (int i = 0; i < fileList.length; i++) {
						if(fileList[i].indexOf(pbid)>=0){
							out.print("	<img class='imgphoto' src=' "+filepath + "/" + fileList[i]+"'");
							out.print("	style='height: 150px; margin: 3px'>");
						}
					}
				}

				//得到web根路径//绝对路径
				//getServletContext().getRealPath("/")得到web应用的根路径
				// D:\web\excel，“D:\web”是web应用的根路径，“excel”是根目录下的文件夹
//				String Save_Location = getServletContext().getRealPath("/");
//				Save_Location = FindFile.getParentPath(Save_Location);
//				imgfiles = FindFile.searchPath(Save_Location + "/pbimg/", pbid);
				//out.print(Save_Location);
			}
			
			
			out.print("</div>");
			//<b class="xbottom"><b class="xb4"></b><b class="xb3"></b><b
			//	class="xb2"></b><b class="xb1"></b>
			//</b>
			out.print("</div>");
			
			out.print("</td></tr></table>");

			out.flush();
			out.close();
			// form.reset(mapping, request);
		} catch (Exception e) {
			out.print("数据访问出错!");
			System.err.println(e);
		}
	}

}
